package edu.bbu.license.client.android.gui.files.dialogs;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import edu.bbu.license.client.android.R;
import edu.bbu.license.client.android.controller.files.FilesController;
import edu.bbu.license.client.android.gui.BaseActivity;

public class NewFolderDialogActivity extends BaseActivity implements NewFolderDialogView {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dialog_new_folder_activity);
		
		initErrorHandler();		
		FilesController.getInstance().atach(this);
	}
	
	private void initErrorHandler() {
		((TextView) findViewById(R.id.new_directory_dialog_input_text)).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				findViewById(R.id.new_directory_dialog_error_label).setVisibility(View.GONE);
				findViewById(R.id.new_directory_dialog_create_button).setVisibility(View.VISIBLE);
			}
		});
	}

	@Override
	public void closeDialogView() {
		finish();
	}
	
	@Override
	public void showError(int messageID) {
		showError(getString(messageID));
	}
	
	@Override
	public void showError(final String errorMessage) {
		runOnUiThread(new Runnable() {
			
			@Override
			public void run() {
				((TextView) findViewById(R.id.new_directory_dialog_error_label)).setText(errorMessage);
				
				findViewById(R.id.new_directory_dialog_create_button).setVisibility(View.GONE);
				findViewById(R.id.new_directory_dialog_error_label).setVisibility(View.VISIBLE);
			}
		});
	}
	
	@Override
	public String getNewFolderName() {
		return ((TextView) findViewById(R.id.new_directory_dialog_input_text)).getText().toString();
	}

	@Override
	public void setCreateButtonListener(OnClickListener onClickListener) {
		findViewById(R.id.new_directory_dialog_create_button).setOnClickListener(onClickListener);	
	}
}
